from pygame.locals import *
import pygame
from Constants import *

class Stack:
    def __init__(self, x, y, shadowImages):
        self.southShadow = None
        self.northShadow = None
        self.shadowImages = shadowImages
        self.canvas = pygame.Surface((STACK_WIDTH, STACK_HEIGHT)).convert()
        self.colorkey = (255,255,0)
        self.canvas.fill(self.colorkey)
        self.canvas.set_colorkey(self.colorkey)
        self.x = x * TILE_WIDTH
        self.y = y * (TILE_Y_SPACING)
        self.blocks = []
        self.locked = False
    
    def render(self, screen):
        if self.southShadow:
            screen.blit(self.shadowImages['Shadow South'], (self.x, self.y + self.southShadow))
        screen.blit(self.canvas, (self.x, self.y))
        if self.northShadow:
            screen.blit(self.shadowImages['Shadow North'], (self.x, self.y + self.northShadow))

        
    def drawShadow(self, name, screen, z):
        yPos = (TILE_DEPTH * MAP_Z) - (TILE_DEPTH * z)
        screen.blit(self.shadowImages[name], (0, yPos))
        
    def update(self, adjacentHeights):
        screen= self.canvas
        screen.fill(self.colorkey)
        blocksInPile = len(self.blocks)
        # blocks
        
        yPos = TILE_DEPTH * MAP_Z
        for z in xrange(blocksInPile):
            yPos = yPos - TILE_DEPTH
            self.blocks[z].render(screen, 0, yPos)
            if adjacentHeights[SOUTH_WEST_DIR] >= z:
                self.drawShadow('Shadow Side West', screen, z)
        
        self.updateShadows(self.canvas, adjacentHeights, blocksInPile)
        
    def updateShadows(self, screen, adjacentHeights, blocksInPile):
        self.southShadow = None
        self.northShadow = None
        if adjacentHeights[NORTH_EAST_DIR] > blocksInPile:
            self.drawShadow('Shadow North East', screen, blocksInPile)
        blocksNorth = adjacentHeights[NORTH_DIR]
        if blocksNorth > 0:
            if blocksNorth > blocksInPile:
                #NOTE: this calculation is incorrect, only produces the correct shadow for one z level
                self.northShadow = (TILE_DEPTH * MAP_Z) - (TILE_DEPTH * blocksInPile)
            elif blocksNorth < blocksInPile:
                self.southShadow = (TILE_DEPTH * MAP_Z) - (TILE_DEPTH * blocksInPile) - SOUTH_SHADOW_HEIGHT
        blocksEast = adjacentHeights[EAST_DIR]
        if blocksEast > blocksInPile:
            self.drawShadow('Shadow East', screen, blocksInPile)
        blocksWest = adjacentHeights[WEST_DIR]
        if blocksWest > blocksInPile:
            self.drawShadow('Shadow West', screen, blocksInPile)
        blocksSouthWest = adjacentHeights[SOUTH_WEST_DIR]
    
    def dropBlock(self, block):
        if self.locked:
            return block
        else:
            self.blocks.append(block)
        
    def grabBlock(self):
        if self.locked:
            return None
        else:
            return self.blocks.pop()
    
    def size(self):
        return len(self.blocks)


